<?php
/**
 * Force check logon
 */
function checkLogin ()
{
	global $user;
	$cookie = new cookieClass(COOKIE_NAME, COOKIE_TIME, true);
	$session = $cookie->ReadCookie('session');
	if (empty($session) || $user['is_guest'] || $user['session_id'] != $session)
	{
		header('location: ' . APP_URL . '/signin');
		exit();
	}
}
/**
 * @param string $session
 */
function checkSession ($session)
{
	global $user;
	$session = trim($session);
	if (empty($session) || ($session != $user['session_id']))
	{
		die('{"status":"error","message":"invalid session"}');
	}
}
/**
 * @return boolean
 */
function updateSession ()
{
	global $db, $uid;
	if (!$uid) 
		return false;
	return $db->updateRow('bbs_members', array('uid' => $uid), array('lastactivity' => getTime()));
}
/**
 * @return number
 */
function getTime ()
{
	return time() + (TIME_OFFSET * 3600);
}
/**
 * @param string $string
 * @param number $charlimit
 * @return string
 */
function limit_string ($string, $charlimit)
{
	if (utf8_strlen($string) <= $charlimit)
		return $string;
	return utf8_substr($string, 0, $charlimit - 3) . '...';
}
/**
 * @param number $sec
 * @param boolean $relative
 * @return string|string|string|string|string|string
 */
function getStrTime ($sec, $relative = true)
{
	$sec -= TIME_OFFSET;
	//return date(TIME_FORMAT, $sec - TIME_OFFSET);
	$strYear = date("Y", $sec) + 543;
	$strMonth = date("n", $sec);
	$strDay = date("j", $sec);
	$strHour = date("H", $sec);
	$strMinute = date("i", $sec);
	$strSeconds = date("s", $sec);
    $strMonthCut = array("", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
	$strMonthThai = $strMonthCut[$strMonth];
	if ($relative)
	{
		$diffSecs = getTime() - $sec;
		if ($diffSecs < 60)
		{
            return floor($diffSecs) . ' วินาทีที่แล้ว';
		}
		elseif ($diffSecs > 60 && $diffSecs < 3600)
		{
            return floor($diffSecs / 60) . ' นาทีที่แล้ว';
        } elseif ($diffSecs > 3600 && $diffSecs < 86400)
        {
            return floor($diffSecs / 3600) . ' ชั่วโมงที่แล้ว';
        } elseif ($diffSecs > 86400 && $diffSecs < 172800)
        {
            return 'เมื่อวานนี้ ' . $strHour . ':' . $strMinute . ' น.';
		}
		else
		{
			return "$strHour:$strMinute น. $strDay $strMonthThai $strYear";
		}
	}
	return "$strHour:$strMinute น. $strDay $strMonthThai $strYear";
}
/**
 * @return string|Ambigous <string, number>
 */
function getUserId ()
{
	global $db, $uid;
	$cookie = new cookieClass(COOKIE_NAME, COOKIE_TIME, true);
	$session = $cookie->ReadCookie('session');
	if(empty($session))
	{
		$uid = false;
		return false;
	}
	$res = $db->getRow('
		SELECT uid 
		FROM bbs_members
		WHERE session_id = %1
		LIMIT 1
		', $session);
	$uid = empty($res) ? false : (int) $res['uid'];
	return $uid;
}
/**
 * @param number $currentPage
 * @return Ambigous <number, multitype:number string>
 */
function getPages ($currentPage)
{
	global $db, $settings;
	$pages = array('current' => 1, 'total' => 1, 'prev' => false, 'next' => false);
	$req = $db->getRow('
			SELECT COUNT(topic_id) as count
			FROM bbs_topics
		');
	$totalPosts = $req['count'];
	if ($totalPosts > 0)
	{
		$pages['current'] = $currentPage + 1;
		$pages['total'] = ceil($totalPosts / $settings['alltopic_display']);
		if ($currentPage > 0)
		{
			$pages['prev'] = $currentPage;
		}
		if ($totalPosts > $settings['alltopic_display'] * $currentPage + $settings['alltopic_display'])
		{
			$pages['next'] = $currentPage + 2;
		}
	}
	return $pages;
}
/**
 * @param number $id
 * @return string|string
 */
function getTopic ($id)
{
	global $db, $bbcode;
	$res = $db->getRow('
			SELECT t.*, m.realname, m.avatar
			FROM bbs_topics t
			INNER JOIN bbs_members m
			ON m.uid = t.topic_poster
			WHERE t.topic_id = %1
			LIMIT 1
		', $id);
	if (empty($res))
		return false;
	updateTopicView($id);
	$res['avatar'] = empty($res['avatar']) ? NO_AVATAR : $res['avatar'];
	$res['topic_title_raw'] = $res['topic_title'];
	$res['topic_title'] = limit_string($res['topic_title'], 50);
	$res['topic_text'] = $bbcode->coverpass($res['topic_text']);
	$res['topic_time'] = getStrTime($res['topic_time']);
	$res['edit_time'] = empty($res['edit_time']) ? '' : getStrTime($res['edit_time']);
	return $res;
}
/**
 * @return string|string
 */
function getLastTopics ()
{
	global $db, $settings;
	$req = $db->query('
			SELECT t.*, m.realname
			FROM bbs_topics t
			INNER JOIN bbs_members m
			ON m.uid = t.topic_poster
			ORDER BY t.topic_id DESC
			LIMIT %1
		', $settings['lasttopic_display']);
	if (empty($req))
		return false;
	foreach ($req as $r)
	{
		$r['topic_title'] = limit_string($r['topic_title'], 25);
		$r['topic_time'] = getStrTime($r['topic_time']);
		$ret[] = $r;
	}
	return $ret;
}
/**
 * @return string|string
 */
function getHotTopics ()
{
	global $db, $settings;
	$req = $db->query('
			SELECT t.*, m.realname
			FROM bbs_topics t
			INNER JOIN bbs_members m
			ON m.uid = t.topic_poster
			ORDER BY t.gift_count DESC
			LIMIT %1
		', $settings['hottopic_display']);
	if (empty($req))
		return false;
	foreach ($req as $r)
	{
		$r['topic_title'] = limit_string($r['topic_title'], 25);
		$r['topic_time'] = getStrTime($r['topic_time']);
		$ret[] = $r;
	}
	return $ret;
}
/**
 * @param number $page
 * @return string|string
 */
function getAllTopics ($page = 0)
{
	global $db, $settings;
	$currentPage = abs(intval($page));
	$res = $db->query('
			SELECT t.*, m.realname, p.post_time
			FROM bbs_topics t
			INNER JOIN bbs_members m
			ON m.uid = t.topic_poster
			LEFT JOIN bbs_posts p
			ON p.post_id = t.last_post_id
			ORDER BY IFNULL(p.post_time, t.topic_time) DESC
			LIMIT %1, %2
		', $currentPage * $settings['alltopic_display'], $settings['alltopic_display']);
	if (empty($res))
		return false;
	foreach ($res as $r)
	{
		$r['topic_title_raw'] = $r['topic_title'];
		$r['topic_title'] = limit_string($r['topic_title'], 60);
		$r['topic_time'] = empty($r['post_time']) ? getStrTime($r['topic_time']) : getStrTime($r['post_time']);
		$ret[] = $r;
	}
	return $ret;
}
/**
 * @return multitype:string
 */
function loadTheme() 
{
	global $db, $uid, $theme;
	$req = $db->getRow('SELECT * FROM bbs_themes WHERE uid = %1', $uid);
	if(!empty($req['bgimage']))
	{
		$req['bgimage'] = getBackground($req['bgimage']);
	}
	$theme = $req;
}
/**
 * @return multitype:string
 */
function loadSettings()
{
	global $db, $settings;
	$req = $db->query('SELECT * FROM bbs_settings');
	foreach($req as $row)
	{
		$settings[$row['key']] = $row['value'];
	}
}
/**
 * @return multitype:multitype:string  
 */
function getBackground($id = false) 
{
	$themes =  array(
		array(
			'id' => '1',
			'thumb' => 'sunsetintuscany-thumb.jpg',
			'full' => 'sunsetintuscany-full.jpg'	
		),
		array(
			'id' => '2',
			'thumb' => 'thefence-thumb.jpg',
			'full' => 'thefence-full.jpg'
		),
		array(
			'id' => '3',
			'thumb' => 'alittlemotivation-thumb.jpg',
			'full' => 'alittlemotivation-full.jpg'
		),
		array(
			'id' => '4',
			'thumb' => 'stonehenge-thumb.jpg',
			'full' => 'stonehenge-full.jpg'
		)
	);
	if($id)
	{
		foreach($themes as $t)
		{
			if($id == $t['id'])
				return $t;
		}
	}
	else
		return $themes;
}
/**
 * @param string $title
 * @param string $text
 * @param number $poster
 * @param number $time
 * @param number $icon
 * @return boolean
 */
function addTopic ($title, $text, $poster, $time, $icon)
{
	global $db;
	if (empty($title) || empty($text))
		return false;
	$data = array('topic_title' => $title, 'topic_text' => $text, 'topic_poster' => $poster, 'topic_time' => $time, 'topic_icon' => $icon, 'topic_view' => 0, 'posts_count' => 0);
	return $db->insertRow('bbs_topics', $data);
}
/**
 * 
 * @param number $id
 * @param string $title
 * @param string $text
 * @param number $editor
 * @param number $time
 * @param number $icon
 * @return boolean
 */
function editTopic ($id, $title, $text, $editor, $time, $icon)
{
	global $db, $user;
	if (empty($id) || empty($title) || empty($text))
		return false;
	$topics = $db->getRow('
			SELECT topic_poster
			FROM bbs_topics
			WHERE topic_id = %1
			LIMIT 1
		', array($id));
	if (empty($topics))
		return false;
	$poster = (int) $topics['topic_poster'];
	$canEdit = $editor == $poster || $user['is_admin'];
	if (! $canEdit)
		return false;
	$data = array('topic_title' => $title, 'topic_text' => $text, 'topic_icon' => $icon, 'edit_time' => $time);
	return $db->updateRow('bbs_topics', array('topic_id' => $id), $data);
}
/**
 * @param number $id
 * @param number $deletor
 * @return string|string|string|boolean
 */
function deleteTopic ($id, $deletor)
{
	global $db, $user;
	if (empty($id) || empty($deletor))
		return false;
	$topics = $db->getRow('
			SELECT topic_poster
			FROM bbs_topics
			WHERE topic_id = %1
			LIMIT 1
		', array($id));
	if (empty($topics))
		return false;
	$poster = (int) $topics['topic_poster'];
	$canDelete = $deletor == $poster || $user['is_admin'];
	if (! $canDelete)
		return false;
	return $db->query('
			DELETE FROM bbs_topics
			WHERE topic_id = %1
		', array($id)) && $db->query('
			DELETE FROM bbs_posts
			WHERE topic_id = %1
		', array($id));
}
/**
 * @param number $id
 * @return boolean
 */
function updateTopicView ($id)
{
	global $db;
	if (empty($id))
		return false;
	$topics = $db->getRow('
			SELECT topic_view, gift
			FROM bbs_topics
			WHERE topic_id = %1
		', array($id));
	$views = $topics['topic_view'] + 1;
	$gift_count = empty($topics['gift']) ? 0 : count(explode(',', $topics['gift']));
	$data = array('topic_view' => (int) $views, 'gift_count' => (int) $gift_count);
	return $db->updateRow('bbs_topics', array('topic_id' => $id), $data);
}
/**
 * @param number $topic_id
 * @param number $post_id
 * @return string
 */
function updateLastPost ($topic_id, $post_id)
{
	global $db;
	if (empty($topic_id) || empty($post_id))
		return false;
	$db->updateRow('bbs_topics', array('topic_id' => $topic_id), array('last_post_id' => $post_id));
}
/**
 * @param number $id
 * @param string $option
 * @return boolean
 */
function updatePostCount ($id, $option)
{
	global $db;
	if (empty($id) || ! in_array($option, array('+', '-')))
		return false;
	$topics = $db->getRow('
			SELECT posts_count
			FROM bbs_topics
			WHERE topic_id = %1
		', array($id));
	$counts = ($option == '+') ? $topics['posts_count'] + 1 : $topics['posts_count'] - 1;
	$data = array('posts_count' => (int) $counts);
	return $db->updateRow('bbs_topics', array('topic_id' => $id), $data);
}
/**
 * @param number $id
 * @return Ambigous <string, unknown>
 */
function getPost ($id)
{
	global $db;
	$res = $db->getRow('
			SELECT *
			FROM bbs_posts
			WHERE post_id = %1
			LIMIT 1
		', array($id));
	return empty($res) ? false : $res;
}
/**
 * @param number $id
 * @return string|string
 */
function getPosts ($id)
{
	global $db, $bbcode, $uid;
	$res = $db->query('
			SELECT p.*, m.realname, m.avatar
			FROM bbs_posts p
			INNER JOIN bbs_members m
			ON m.uid = p.poster_id
			WHERE p.topic_id = %1
			ORDER BY p.post_time ASC
		', array($id));
	if (empty($res))
		return false;
	$counter = 1;
	foreach ($res as $r)
	{
		$r['counter'] = $counter ++;
		$r['post_text'] = $bbcode->coverpass($r['post_text']);
		$r['post_time'] = getStrTime($r['post_time']);
		$r['edit_time'] = empty($r['edit_time']) ? '' : getStrTime($r['edit_time']);
		$r['avatar'] = empty($r['avatar']) ? NO_AVATAR : $r['avatar'];
		$r['can_gift'] = true;
		$gift = false;
		if (! empty($r['gift']))
		{
			foreach (explode(',', $r['gift']) as $k => $v)
			{
				$profile = getUserProfile($v);
				$sep = ($k == 0) ? '' : ', ';
				$gift .= $sep . '<a href="' . APP_URL . '/user/' . $profile['uid'] . '" title="ดูโปรไฟล์ของ ' . $profile['realname'] . '">' . $profile['realname'] . '</a>';
			}
			if (in_array($uid, explode(',', $r['gift'])))
				$r['can_gift'] = false;
			else
				$r['can_gift'] = true;
			$r['gift_count'] = count(explode(',', $r['gift']));
			$r['gift_text'] = $gift;
		}
		$ret[] = $r;
	}
	return $ret;
}
/**
 * @param number $id
 * @param number $poster
 * @param string $text
 * @param number $time
 * @return string
 */
function addPost ($id, $poster, $text, $time)
{
	global $db;
	if (empty($id) || empty($text))
		return false;
	$data = array('topic_id' => $id, 'poster_id' => $poster, 'post_text' => $text, 'post_time' => $time);
	$db->insertRow('bbs_posts', $data);
	$post_id = $db->insertId();
	updatePostCount($id, '+');
	updateLastPost($id, $post_id);
}
/**
 * @param number $id
 * @param number $editor
 * @param string $text
 * @param number $time
 * @return boolean
 */
function editPost ($id, $editor, $text, $time)
{
	global $db, $user;
	if (empty($id) || empty($text))
		return false;
	$posts = $db->getRow('
			SELECT poster_id
			FROM bbs_posts
			WHERE post_id = %1
			LIMIT 1
		', array($id));
	if (empty($posts))
		return false;
	$poster = (int) $posts['poster_id'];
	$canEdit = $editor == $poster || $user['is_admin'];
	if (! $canEdit)
		return false;
	$data = array('post_text' => $text, 'edit_time' => $time);
	return $db->updateRow('bbs_posts', array('post_id' => $id), $data);
}
/**
 * @param number $id
 * @param number $deletor
 * @return string|string|string|boolean
 */
function deletePost ($id, $deletor)
{
	global $db, $user;
	if (empty($id))
		return false;
	$posts = $db->getRow('
			SELECT poster_id, topic_id
			FROM bbs_posts
			WHERE post_id = %1
			LIMIT 1
		', array($id));
	if (empty($posts))
		return false;
	$poster = (int) $posts['poster_id'];
	$topic_id = (int) $posts['topic_id'];
	$canDelete = $deletor == $poster || $user['is_admin'];
	if (! $canDelete)
		return false;
	$req = $db->getRow('
			SELECT post_id
			FROM bbs_posts
			WHERE topic_id = %1
			AND post_id < %2
			ORDER BY post_time DESC
			LIMIT 1
		', array($topic_id, $id));
	$last_post = $req['post_id'];
	return $db->query('
			DELETE FROM bbs_posts
			WHERE post_id = %1
		', array($id)) && updatePostCount($topic_id, '-') && updateLastPost($topic_id, $last_post);
}
/**
 * @param number $uid
 * @return string|string|multitype:
 */
function getUserProfile ($id)
{
	global $db, $uid, $user;
	if (!$id || $id < 1)
	{
		$user['is_guest'] = true;
		return $user;
	}
	$res = $db->getRow('
			SELECT *
			FROM bbs_members
			WHERE uid = %1
			LIMIT 1
		', array($id));
	if (empty($res))
		return false;
	if ($res['uid'] == 1)
		$res['is_admin'] = true;
	$res['is_guest'] = false;
	$res['avatar'] = empty($res['avatar']) ? NO_AVATAR : $res['avatar'];
	$res['buddylist'] = array();
	$req = $db->query('
			SELECT friendid
			FROM bbs_buddy
			WHERE uid = %1
		', $res['uid']);
	foreach ($req as $r)
	{
		$res['buddylist'][] = $r['friendid'];
	}
	if($id == $uid)
		$user = $res;
	return $res;
}
function getUserOnline()
{
	global $db, $settings;
	$online_timeout = getTime() - 60;
	$onlines = $db->query('
		SELECT uid, IFNULL(realname, username) as realname 
		FROM bbs_members 
		WHERE lastactivity >= %1 
		ORDER BY lastactivity
	', $online_timeout);
	return $onlines;
}
/**
 * @param number $id
 * @param string $option
 * @return boolean
 */
function updateGiftCount ($id, $option)
{
	global $db;
	if (empty($id) || ! in_array($option, array('+', '-')))
		return false;
	$res = $db->getRow('
			SELECT gift
			FROM bbs_members
			WHERE uid = %1
			LIMIT 1
		', array($id));
	if (empty($res))
		return false;
	$gifts = ($option == '+') ? $res['posts'] + 1 : $res['posts'] - 1;
	$data = array('gift' => (int) $gifts);
	return $db->updateRow('bbs_members', array('uid' => $id), $data);
}
/**
 * @param number $id
 * @param string $realname
 * @param string $sex
 * @param string $email
 * @param string $avatar
 * @return boolean
 */
function editProfile ($id, $realname, $sex, $email)
{
	global $db;
	$data = array('realname' => $realname, 'email' => $email, 'sex' => $sex);
	return $db->updateRow('bbs_members', array('uid' => $id), $data);
}
/**
 * @param number $id
 * @param string $avatar
 * @return boolean
 */
function editPhoto ($id, $avatar)
{
	global $db;
	return $db->updateRow('bbs_members', array('uid' => $id), array('avatar' => $avatar));
}
/**
 * @param number $id
 * @param string $password
 * @return boolean:
 */
function editPassword ($id, $password)
{
	global $db;
	$data = array('password' => $password);
	return $db->updateRow('bbs_members', array('uid' => $id), $data);
}
/**
 * @return multitype:string 
 */
function getIcons ()
{
	return array('0', '1', '2', '3', '4', '5', '6');
}
/**
 * @return string|string
 */
function getNews ()
{
	global $db, $bbcode, $news;
	$req = $db->query('SELECT * FROM bbs_news WHERE is_show = 1 ORDER BY news_id');
	if (empty($req))
		return false;
	foreach ($req as $n)
	{
		$n['news_text'] = $bbcode->coverpass($n['news_text']);
		$n['news_time'] = getStrTime($n['news_time']);
		$result[] = $n;
	}
	$news = $result;
}
/**
 * @param number $nw
 * @param number $nh
 * @param string $source
 * @param string $stype
 * @param string $dest
 */
function cropImage ($nw, $nh, $source, $stype, $dest)
{
	$size = getimagesize($source);
	$w = $size[0];
	$h = $size[1];
	switch ($stype)
	{
		case 'gif':
			$simg = imagecreatefromgif($source);
			break;
		case 'jpeg':
		case 'jpg':
			$simg = imagecreatefromjpeg($source);
			break;
		case 'png':
			$simg = imagecreatefrompng($source);
			break;
	}
	$dimg = imagecreatetruecolor($nw, $nh);
	$wm = $w / $nw;
	$hm = $h / $nh;
	$h_height = $nh / 2;
	$w_height = $nw / 2;
	if ($w > $h)
	{
		$adjusted_width = $w / $hm;
		$half_width = $adjusted_width / 2;
		$int_width = $half_width - $w_height;
		imagecopyresampled($dimg, $simg, - $int_width, 0, 0, 0, $adjusted_width, $nh, $w, $h);
	}
	elseif (($w < $h) || ($w == $h))
	{
		$adjusted_height = $h / $wm;
		$half_height = $adjusted_height / 2;
		$int_height = $half_height - $h_height;
		imagecopyresampled($dimg, $simg, 0, - $int_height, 0, 0, $nw, $adjusted_height, $w, $h);
	}
	else
	{
		imagecopyresampled($dimg, $simg, 0, 0, 0, 0, $nw, $nh, $w, $h);
	}
	if (file_exists($dest))
		unlink($dest);
	imagejpeg($dimg, $dest, 100);
}
/**
 * @param number $length
 * @return string
 */
function genChar ($length = 8)
{
	$chars = '0123456789abcdefghjkmnoprstvwxyz';
	$code = '';
	for ($i = 0; $i < $length; $i ++)
	{
		$code .= substr($chars, (((int) mt_rand(0, strlen($chars))) - 1), 1);
	}
	return $code;
}
/**
 * @param string $str
 * @return number
 */
function utf8_strlen ($str)
{
	$count = 0;
	for ($i = 0; $i < strlen($str); $i ++)
	{
		$value = ord($str[$i]);
		if ($value > 127)
		{
			if ($value >= 192 && $value <= 223)
				$i ++;
			elseif ($value >= 224 && $value <= 239)
				$i = $i + 2;
			elseif ($value >= 240 && $value <= 247)
				$i = $i + 3;
			else
				die('Not a UTF-8 compatible string');
		}
		$count ++;
	}
	return $count;
}
function utf8_substr ($str, $start)
{
	preg_match_all("/./su", $str, $ar);
	if (func_num_args() >= 3)
	{
		$end = func_get_arg(2);
		return join("", array_slice($ar[0], $start, $end));
	}
	else
	{
		return join("", array_slice($ar[0], $start));
	}
}
/**
 * @return number
 * http://www.codecall.net
 */
function slog_time ()
{
	$mtime = microtime();
	$mtime = explode(" ", $mtime);
	$mtime = $mtime[1] + $mtime[0];
	return $mtime;
}
/**
 * @param number $starttime
 * @return number
 */
function elog_time ($starttime)
{
	$mtime = microtime();
	$mtime = explode(" ", $mtime);
	$mtime = $mtime[1] + $mtime[0];
	$endtime = $mtime;
	return $endtime - $starttime;
}
/**
 * @param string $message
 * @return string
 * http://www.zone-it.com/78026
 */
function coms_post_user_bbcode_tag ($message)
{
	global $db;
	$req = $db->getRow('
			SELECT realname
			FROM bbs_members
			WHERE uid = %1
			LIMIT 1
		', $message[1]);
	return (empty($req)) ? '[color=red]ไม่พบสมาชิกไอดี: [b]' . $message[1] . '[/b]![/color]' : '<a href="' . APP_URL . '/user/' . $message[1] . '" title="ดูโปรไฟล์ของ ' . $req['realname'] . '">' . $req['realname'] . '</a>';
}
/**
 * @param string $message
 * @return string
 * http://www.zone-it.com/50119
 */
function zone_it_topic_bbcode_tag ($message)
{
	global $db;
	$req = $db->getRow('
			SELECT t.topic_title, m.uid, m.realname
			FROM bbs_topics t
			LEFT JOIN bbs_members m
			ON m.uid = t.topic_poster
			WHERE t.topic_id = %1
			LIMIT 1', $message[1]);
	return (empty($req)) ? '[color=red]ไม่พบกระทู้ไอดี: [b]' . $message[1] . '[/b]![/color]' : '<a href="' . APP_URL . '/topic/' . $message[1] . '" title="' . $req['topic_title'] . '">' . $req['topic_title'] . '</a> โดย <a href="' . APP_URL . '/user/' . $req['uid'] . '" title="ดูโปรไฟล์ของ ' . $req['realname'] . '">' . $req['realname'] . '</a>';
}
/**
 * @author pdnb
 * Captcha Class
 */
class Captcha
{
	var $session;
	function random ()
	{
		$_SESSION[$this->session] = array();
		for ($i = 0; $i < 2; $i ++)
		{
			$_SESSION[$this->session][] = rand(1, 20);
		}
		$_SESSION[$this->session]['result'] = $_SESSION[$this->session][0] + $_SESSION[$this->session][1];
		return $_SESSION[$this->session];
	}
	function display ()
	{
		$number = $this->random();
        return $number[0] . ' บวก ' . $number[1] . '  เท่ากับ?'
		;
	}
}
?>